import axios from 'axios'
import token from './token.ts'
import {ElMessage} from "element-plus"

/**
 * 创建请求类
 */
const request = axios.create({
    baseURL: "",
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    },
    timeout: 20000,
    otherParams: {
        isLoading: false
    }
})

request.interceptors.request.use((config: any) => {
    config.headers['Authorization'] = 'Bearer ' + token.get()
    return config
}, (error: any) => {
    return Promise.reject(error)
})

/**
 * 响应拦截器
 */
request.interceptors.response.use((res: any) => {
    if (res.data.data) {
        return res.data.data
    } else {
        return res.data
    }
}, (error: any) => {
    if (error.response) {
        if (error.response.code == 404) {
            ElMessage.error(`错误代码：${error.response.code}；错误信息：找不到接口！`)
        } else {
            ElMessage.error(`${error.response.message}`)
        }
    }
    return Promise.reject(error)
})

/**
 * 暴露
 */
export {
    request
}